home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / man / cat.1 / perlrun.1 < prev    next >
Text File  |  1995-07-25  |  23KB  |  529 lines

  1.  
  2.  
  3.  
  4.      PPPPEEEERRRRLLLLRRRRUUUUNNNN((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLRRRRUUUUNNNN((((1111))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.           perlrun - how to execute the Perl interpreter
  10.  
  11.      SSSSYYYYNNNNOOOOPPPPSSSSIIIISSSS
  12.           ppppeeeerrrrllll [switches] filename args
  13.  
  14.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  15.           Upon startup, Perl looks for your script in one of the
  16.           following places:
  17.  
  18.           1.  Specified line by line via ----eeee switches on the command
  19.               line.
  20.  
  21.           2.  Contained in the file specified by the first filename on
  22.               the command line.  (Note that systems supporting the #!
  23.               notation invoke interpreters this way.)
  24.  
  25.           3.  Passed in implicitly via standard input.  This only
  26.               works if there are no filename arguments--to pass
  27.               arguments to a STDIN script you must explicitly specify
  28.               a "-" for the script name.
  29.  
  30.           With methods 2 and 3, Perl starts parsing the input file
  31.           from the beginning, unless you've specified a ----xxxx switch, in
  32.           which case it scans for the first line starting with #! and
  33.           containing the word "perl", and starts there instead.  This
  34.           is useful for running a script embedded in a larger message.
  35.           (In this case you would indicate the end of the script using
  36.           the __END__ token.)
  37.  
  38.           As of Perl 5, the #! line is always examined for switches as
  39.           the line is being parsed.  Thus, if you're on a machine that
  40.           only allows one argument with the #! line, or worse, doesn't
  41.           even recognize the #! line, you still can get consistent
  42.           switch behavior regardless of how Perl was invoked, even if
  43.           ----xxxx was used to find the beginning of the script.
  44.  
  45.           Because many operating systems silently chop off kernel
  46.           interpretation of the #! line after 32 characters, some
  47.           switches may be passed in on the command line, and some may
  48.           not; you could even get a "-" without its letter, if you're
  49.           not careful.  You probably want to make sure that all your
  50.           switches fall either before or after that 32 character
  51.           boundary.  Most switches don't actually care if they're
  52.           processed redundantly, but getting a - instead of a complete
  53.           switch could cause Perl to try to execute standard input
  54.           instead of your script.  And a partial ----IIII switch could also
  55.           cause odd results.
  56.  
  57.           Parsing of the #! switches starts wherever "perl" is
  58.           mentioned in the line.  The sequences "-*" and "- " are
  59.           specifically ignored so that you could, if you were so
  60.  
  61.  
  62.  
  63.      Page 1                                          (printed 6/30/95)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      PPPPEEEERRRRLLLLRRRRUUUUNNNN((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLRRRRUUUUNNNN((((1111))))
  71.  
  72.  
  73.  
  74.           inclined, say
  75.  
  76.               #!/bin/sh -- # -*- perl -*- -p
  77.               eval 'exec perl $0 -S ${1+"$@"}'
  78.                   if 0;
  79.  
  80.           to let Perl see the ----pppp switch.
  81.  
  82.           If the #! line does not contain the word "perl", the program
  83.           named after the #! is executed instead of the Perl
  84.           interpreter.  This is slightly bizarre, but it helps people
  85.           on machines that don't do #!, because they can tell a
  86.           program that their SHELL is /usr/bin/perl, and Perl will
  87.           then dispatch the program to the correct interpreter for
  88.           them.
  89.  
  90.           After locating your script, Perl compiles the entire script
  91.           to an internal form.  If there are any compilation errors,
  92.           execution of the script is not attempted.  (This is unlike
  93.           the typical shell script, which might run partway through
  94.           before finding a syntax error.)
  95.  
  96.           If the script is syntactically correct, it is executed.  If
  97.           the script runs off the end without hitting an _e_x_i_t() or
  98.           _d_i_e() operator, an implicit exit(0) is provided to indicate
  99.           successful completion.
  100.  
  101.           SSSSwwwwiiiittttcccchhhheeeessss
  102.  
  103.           A single-character switch may be combined with the following
  104.           switch, if any.
  105.  
  106.               #!/usr/bin/perl -spi.bak    # same as -s -p -i.bak
  107.  
  108.           Switches include:
  109.  
  110.           ----0000_d_i_g_i_t_s
  111.                specifies the record separator ($/) as an octal number.
  112.                If there are no digits, the null character is the
  113.                separator.  Other switches may precede or follow the
  114.                digits.  For example, if you have a version of ffffiiiinnnndddd
  115.                which can print filenames terminated by the null
  116.                character, you can say this:
  117.  
  118.                    find . -name '*.bak' -print0 | perl -n0e unlink
  119.  
  120.                The special value 00 will cause Perl to slurp files in
  121.                paragraph mode.  The value 0777 will cause Perl to
  122.                slurp files whole since there is no legal character
  123.                with that value.
  124.  
  125.  
  126.  
  127.  
  128.  
  129.      Page 2                                          (printed 6/30/95)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      PPPPEEEERRRRLLLLRRRRUUUUNNNN((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLRRRRUUUUNNNN((((1111))))
  137.  
  138.  
  139.  
  140.           ----aaaa   turns on autosplit mode when used with a ----nnnn or ----pppp.  An
  141.                implicit split command to the @F array is done as the
  142.                first thing inside the implicit while loop produced by
  143.                the ----nnnn or ----pppp.
  144.  
  145.                    perl -ane 'print pop(@F), "\n";'
  146.  
  147.                is equivalent to
  148.  
  149.                    while (<>) {
  150.                        @F = split(' ');
  151.                        print pop(@F), "\n";
  152.                    }
  153.  
  154.                An alternate delimiter may be specified using ----FFFF.
  155.  
  156.           ----cccc   causes Perl to check the syntax of the script and then
  157.                exit without executing it.
  158.  
  159.           ----dddd   runs the script under the Perl debugger.  See the
  160.                _p_e_r_l_d_e_b_u_g manpage.
  161.  
  162.           ----DDDD_n_u_m_b_e_r
  163.  
  164.           ----DDDD_l_i_s_t
  165.                sets debugging flags.  To watch how it executes your
  166.                script, use ----DDDD11114444.  (This only works if debugging is
  167.                compiled into your Perl.)  Another nice value is
  168.                ----DDDD1111000022224444, which lists your compiled syntax tree.  And
  169.                ----DDDD555511112222 displays compiled regular expressions. As an
  170.                alternative specify a list of letters instead of
  171.                numbers (e.g. ----DDDD11114444 is equivalent to ----DDDDttttllllssss):
  172.  
  173.                        1  p  Tokenizing and Parsing
  174.                        2  s  Stack Snapshots
  175.                        4  l  Label Stack Processing
  176.                        8  t  Trace Execution
  177.                       16  o  Operator Node Construction
  178.                       32  c  String/Numeric Conversions
  179.                       64  P  Print Preprocessor Command for -P
  180.                      128  m  Memory Allocation
  181.                      256  f  Format Processing
  182.                      512  r  Regular Expression Parsing
  183.                     1024  x  Syntax Tree Dump
  184.                     2048  u  Tainting Checks
  185.                     4096  L  Memory Leaks (not supported anymore)
  186.                     8192  H  Hash Dump -- usurps values()
  187.                    16384  X  Scratchpad Allocation
  188.                    32768  D  Cleaning Up
  189.  
  190.  
  191.  
  192.  
  193.  
  194.  
  195.      Page 3                                          (printed 6/30/95)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      PPPPEEEERRRRLLLLRRRRUUUUNNNN((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLRRRRUUUUNNNN((((1111))))
  203.  
  204.  
  205.  
  206.           ----eeee _c_o_m_m_a_n_d_l_i_n_e
  207.                may be used to enter one line of script. If ----eeee is
  208.                given, Perl will not look for a script filename in the
  209.                argument list. Multiple ----eeee commands may be given to
  210.                build up a multi-line script. Make sure to use
  211.                semicolons where you would in a normal program.
  212.  
  213.           ----FFFF_r_e_g_e_x_p
  214.                specifies a regular expression to split on if ----aaaa is
  215.                also in effect.  If regexp has // around it, the
  216.                slashes will be ignored.
  217.  
  218.           ----iiii_e_x_t_e_n_s_i_o_n
  219.                specifies that files processed by the <> construct are
  220.                to be edited in-place.  It does this by renaming the
  221.                input file, opening the output file by the original
  222.                name, and selecting that output file as the default for
  223.                _p_r_i_n_t() statements.  The extension, if supplied, is
  224.                added to the name of the old file to make a backup
  225.                copy.  If no extension is supplied, no backup is made.
  226.                From the shell, saying
  227.  
  228.                    $ perl -p -i.bak -e "s/foo/bar/; ... "
  229.  
  230.                is the same as using the script:
  231.  
  232.                    #!/usr/bin/perl -pi.bak
  233.                    s/foo/bar/;
  234.  
  235.                which is equivalent to
  236.  
  237.                    #!/usr/bin/perl
  238.                    while (<>) {
  239.                        if ($ARGV ne $oldargv) {
  240.                            rename($ARGV, $ARGV . '.bak');
  241.                            open(ARGVOUT, ">$ARGV");
  242.                            select(ARGVOUT);
  243.                            $oldargv = $ARGV;
  244.                        }
  245.                        s/foo/bar/;
  246.                    }
  247.                    continue {
  248.                        print;  # this prints to original filename
  249.                    }
  250.                    select(STDOUT);
  251.  
  252.                except that the ----iiii form doesn't need to compare $ARGV
  253.                to $oldargv to know when the filename has changed.  It
  254.                does, however, use ARGVOUT for the selected filehandle.
  255.                Note that STDOUT is restored as the default output
  256.                filehandle after the loop.
  257.  
  258.  
  259.  
  260.  
  261.      Page 4                                          (printed 6/30/95)
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.      PPPPEEEERRRRLLLLRRRRUUUUNNNN((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLRRRRUUUUNNNN((((1111))))
  269.  
  270.  
  271.  
  272.                You can use eof without parenthesis to locate the end
  273.                of each input file, in case you want to append to each
  274.                file, or reset line numbering (see example in the eof
  275.                entry in the _p_e_r_l_f_u_n_c manpage).
  276.  
  277.           ----IIII_d_i_r_e_c_t_o_r_y
  278.                may be used in conjunction with ----PPPP to tell the C
  279.                preprocessor where to look for include files.  By
  280.                default /usr/include and /usr/lib/perl are searched.
  281.  
  282.           ----llll_o_c_t_n_u_m
  283.                enables automatic line-ending processing.  It has two
  284.                effects:  first, it automatically chomps the line
  285.                terminator when used with ----nnnn or ----pppp, and second, it
  286.                assigns "$\" to have the value of _o_c_t_n_u_m so that any
  287.                print statements will have that line terminator added
  288.                back on.  If _o_c_t_n_u_m is omitted, sets "$\" to the
  289.                current value of "$/".  For instance, to trim lines to
  290.                80 columns:
  291.  
  292.                    perl -lpe 'substr($_, 80) = ""'
  293.  
  294.                Note that the assignment $\ = $/ is done when the
  295.                switch is processed, so the input record separator can
  296.                be different than the output record separator if the ----llll
  297.                switch is followed by a ----0000 switch:
  298.  
  299.                    gnufind / -print0 | perl -ln0e 'print "found $_" if -p'
  300.  
  301.                This sets $\ to newline and then sets $/ to the null
  302.                character.
  303.  
  304.           ----nnnn   causes Perl to assume the following loop around your
  305.                script, which makes it iterate over filename arguments
  306.                somewhat like sssseeeedddd ----nnnn or aaaawwwwkkkk:
  307.  
  308.                    while (<>) {
  309.                        ...             # your script goes here
  310.                    }
  311.  
  312.                Note that the lines are not printed by default.  See ----pppp
  313.                to have lines printed.  Here is an efficient way to
  314.                delete all files older than a week:
  315.  
  316.                    find . -mtime +7 -print | perl -nle 'unlink;'
  317.  
  318.                This is faster than using the -exec switch of ffffiiiinnnndddd
  319.                because you don't have to start a process on every
  320.                filename found.
  321.  
  322.                BEGIN and END blocks may be used to capture control
  323.                before or after the implicit loop, just as in aaaawwwwkkkk.
  324.  
  325.  
  326.  
  327.      Page 5                                          (printed 6/30/95)
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.      PPPPEEEERRRRLLLLRRRRUUUUNNNN((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLRRRRUUUUNNNN((((1111))))
  335.  
  336.  
  337.  
  338.           ----pppp   causes Perl to assume the following loop around your
  339.                script, which makes it iterate over filename arguments
  340.                somewhat like sssseeeedddd:
  341.  
  342.                    while (<>) {
  343.                        ...             # your script goes here
  344.                    } continue {
  345.                        print;
  346.                    }
  347.  
  348.                Note that the lines are printed automatically.  To
  349.                suppress printing use the ----nnnn switch.  A ----pppp overrides a
  350.                ----nnnn switch.
  351.  
  352.                BEGIN and END blocks may be used to capture control
  353.                before or after the implicit loop, just as in awk.
  354.  
  355.           ----PPPP   causes your script to be run through the C preprocessor
  356.                before compilation by Perl.  (Since both comments and
  357.                cpp directives begin with the # character, you should
  358.                avoid starting comments with any words recognized by
  359.                the C preprocessor such as "if", "else" or "define".)
  360.  
  361.           ----ssss   enables some rudimentary switch parsing for switches on
  362.                the command line after the script name but before any
  363.                filename arguments (or before a --------).  Any switch found
  364.                there is removed from @ARGV and sets the corresponding
  365.                variable in the Perl script.  The following script
  366.                prints "true" if and only if the script is invoked with
  367.                a ----xxxxyyyyzzzz switch.
  368.  
  369.                    #!/usr/bin/perl -s
  370.                    if ($xyz) { print "true\n"; }
  371.  
  372.  
  373.           ----SSSS   makes Perl use the PATH environment variable to search
  374.                for the script (unless the name of the script starts
  375.                with a slash).  Typically this is used to emulate #!
  376.                startup on machines that don't support #!, in the
  377.                following manner:
  378.  
  379.                    #!/usr/bin/perl
  380.                    eval "exec /usr/bin/perl -S $0 $*"
  381.                            if $running_under_some_shell;
  382.  
  383.                The system ignores the first line and feeds the script
  384.                to /bin/sh, which proceeds to try to execute the Perl
  385.                script as a shell script.  The shell executes the
  386.                second line as a normal shell command, and thus starts
  387.                up the Perl interpreter.  On some systems $0 doesn't
  388.                always contain the full pathname, so the ----SSSS tells Perl
  389.                to search for the script if necessary.  After Perl
  390.  
  391.  
  392.  
  393.      Page 6                                          (printed 6/30/95)
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.      PPPPEEEERRRRLLLLRRRRUUUUNNNN((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLRRRRUUUUNNNN((((1111))))
  401.  
  402.  
  403.  
  404.                locates the script, it parses the lines and ignores
  405.                them because the variable $running_under_some_shell is
  406.                never true.  A better construct than $* would be
  407.                ${1+"$@"}, which handles embedded spaces and such in
  408.                the filenames, but doesn't work if the script is being
  409.                interpreted by csh.  In order to start up sh rather
  410.                than csh, some systems may have to replace the #! line
  411.                with a line containing just a colon, which will be
  412.                politely ignored by Perl.  Other systems can't control
  413.                that, and need a totally devious construct that will
  414.                work under any of csh, sh or Perl, such as the
  415.                following:
  416.  
  417.                        eval '(exit $?0)' && eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
  418.                        & eval 'exec /usr/bin/perl -S $0 $argv:q'
  419.                                if 0;
  420.  
  421.  
  422.           ----TTTT   forces "taint" checks to be turned on.  Ordinarily
  423.                these checks are done only when running setuid or
  424.                setgid.  See the _p_e_r_l_s_e_c manpage.
  425.  
  426.           ----uuuu   causes Perl to dump core after compiling your script.
  427.                You can then take this core dump and turn it into an
  428.                executable file by using the uuuunnnndddduuuummmmpppp program (not
  429.                supplied).  This speeds startup at the expense of some
  430.                disk space (which you can minimize by stripping the
  431.                executable).  (Still, a "hello world" executable comes
  432.                out to about 200K on my machine.)  If you want to
  433.                execute a portion of your script before dumping, use
  434.                the _d_u_m_p() operator instead.  Note: availability of
  435.                uuuunnnndddduuuummmmpppp is platform specific and may not be available
  436.                for a specific port of Perl.
  437.  
  438.           ----UUUU   allows Perl to do unsafe operations.  Currently the
  439.                only "unsafe" operations are the unlinking of
  440.                directories while running as superuser, and running
  441.                setuid programs with fatal taint checks turned into
  442.                warnings.
  443.  
  444.           ----vvvv   prints the version and patchlevel of your Perl
  445.                executable.
  446.  
  447.           ----wwww   prints warnings about identifiers that are mentioned
  448.                only once, and scalar variables that are used before
  449.                being set.  Also warns about redefined subroutines, and
  450.                references to undefined filehandles or filehandles
  451.                opened readonly that you are attempting to write on.
  452.                Also warns you if you use values as a number that
  453.                doesn't look like numbers, using a an array as though
  454.                it were a scalar, if your subroutines recurse more than
  455.                100 deep, and innumeriable other things.  See the
  456.  
  457.  
  458.  
  459.      Page 7                                          (printed 6/30/95)
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.      PPPPEEEERRRRLLLLRRRRUUUUNNNN((((1111))))  UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000))))  PPPPEEEERRRRLLLLRRRRUUUUNNNN((((1111))))
  467.  
  468.  
  469.  
  470.                _p_e_r_l_d_i_a_g manpage and the _p_e_r_l_t_r_a_p manpage.
  471.  
  472.           ----xxxx _d_i_r_e_c_t_o_r_y
  473.                tells Perl that the script is embedded in a message.
  474.                Leading garbage will be discarded until the first line
  475.                that starts with #! and contains the string "perl".
  476.                Any meaningful switches on that line will be applied
  477.                (but only one group of switches, as with normal #!
  478.                processing).  If a directory name is specified, Perl
  479.                will switch to that directory before running the
  480.                script.  The ----xxxx switch only controls the the disposal
  481.                of leading garbage.  The script must be terminated with
  482.                __END__ if there is trailing garbage to be ignored (the
  483.                script can process any or all of the trailing garbage
  484.                via the DATA filehandle if desired).
  485.  
  486.  
  487.  
  488.  
  489.  
  490.  
  491.  
  492.  
  493.  
  494.  
  495.  
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.  
  507.  
  508.  
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.      Page 8                                          (printed 6/30/95)
  526.  
  527.  
  528.  
  529.